package edu.uwlax.cs.oayonlinestore.client.gui.shared;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.Order;
import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.Order.OrderChangeListener;
import edu.uwlax.cs.oayonlinestore.client.gui.Store;
import edu.uwlax.cs.oayonlinestore.client.gui.shared.Const.OrderStatus;
import edu.uwlax.cs.oayonlinestore.client.utils.Currency;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.vo.OrderSummaryVO;
import edu.uwlax.cs.oayonlinestore.vo.UserDetailsVO;

public class OrderTable extends DekoTable {

	private class LoadHandler extends DefaultAsyncCallback {
		public void onSuccess(Object arg0) {
			loadList((OrderSummaryVO[]) arg0);
		}
	}

	private boolean showName;

	private boolean isSales;

	public OrderTable(boolean showName, boolean isSales) {
		super();
		this.showName = showName;
		this.isSales = isSales;
		addTitle("ID");
		addTitle("Date");
		if (showName)
			addTitle("Name");
		addTitle("Items");
		addTitle("Value");
		addTitle("Status");
		if (isSales) {
			addTitle("");
			addTitle("");
		}
		addTitle("");
		addTitle("");
	}

	private void addItem(OrderSummaryVO o) {
		AddRow r = addRow();
		r.add(o.getId());
		r.add((o.getOrderDate() == null) ? "" : o.getOrderDate().toString());
		if (showName)
			r.add(o.getUser().getFirstname() + " " + o.getUser().getLastname());
		r.add(o.getItemCount());
		r.add(new Currency(o.getTotal()));
		r.add(o.getStatus());
		String det = Store.getFrame().getUsrViewOrder().getAnchor(o.getId());
		r.add(new Hyperlink("details", det));
		if (isSales) {
			String adm = Store.getFrame().getSalOrder().getAnchor(o.getId());
			r.add(new Hyperlink("admin", adm));
			String retex = Store.getFrame().getSalRetex().getAnchor(true,
					o.getId());
			r.add(new Hyperlink("ret/ex", retex));
		}
		if (canEditOrder(o))
			r.add(new Button("edit", new SetPageHandler(o.getId())));
		else
			r.add("");
	}

	private class SetPageHandler extends DefaultAsyncCallback implements
			ClickListener, OrderChangeListener {
		private int orderID;

		public SetPageHandler(int orderID) {
			super();
			this.orderID = orderID;
		}

		public void onClick(Widget arg0) {
			OnlinestoreRService.Util.getInstance().setCurrentOrder(orderID,
					this);
		}

		public void orderChanged(Order order) {
			String items = Store.getFrame().getShopFrame().getCart()
					.getAnchor();
			History.newItem(items);
		}

		public void onSuccess(Object arg0) {
			Order.get().addChangeListener(this);
			Order.get().update();
		}

	}

	private boolean canEditOrder(OrderSummaryVO o) {
		if (isSales)
			return true;
		UserDetailsVO u = User.get();
		boolean ownOrder = u.getId() == o.getUser().getId();
		boolean isCustomer = u.isIsNormalCustomer()
				|| u.isIsIndustrialCustomer();
		return ownOrder && isCustomer
				&& OrderStatus.IN_PROGRESS.equals(o.getStatus());
	}

	public void loadByUser(int userID) {
		OnlinestoreRService.Util.getInstance().getOrderSummaryList(userID,
				new LoadHandler());
	}

	private void loadList(OrderSummaryVO[] list) {
		cleanExceptHeader();
		for (int i = 0; i < list.length; i++)
			addItem(list[i]);
	}

}